热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

C#|BitConverter。ToInt32()方法

C#|BitConverter。ToInt32()方法原文:

C# | BitConverter。ToInt32()方法

原文:https://www . geesforgeks . org/c-sharp-bit converter-to int32-method/

BitConverter。ToInt32(Byte[],Int32)方法用于返回从字节数组中指定位置的四个字节转换而来的 32 位有符号整数。
语法:

public static int ToInt32 (byte[] value, int startIndex);

参数:

值:是字节数组。
起始指数:是数值内的起始位置。

返回值:这个方法返回一个 32 位有符号整数,由 startIndex 开始的两个字节组成。
T3】例外:T5】


  • 参数异常:如果 startIndex 大于或等于值的长度减 3,并且小于或等于值的长度减 1。

  • ArgumentNullException: 如果值为空。

  • argumentoutofrangerexception:如果 startIndex 小于零或大于值的长度减 1。

以下程序说明了位转换器的使用。【方法:
例 1:****

c sharp . c sharp . c sharp . c sharp


// C# program to demonstrate
// BitConverter.ToInt32(Byte[], Int32);
// Method
using System;
class GFG {
// Main Method
public static void Main()
{
    try {
        // Define an array
        // of byte values.
        byte[] bytes = {32, 0, 0, 42, 0,
                        65, 0, 125, 0, 197,
                        0, 168, 3, 41, 4,
                               125, 32 };
        // Display the values of the myArr.
        Console.Write("Initial Array: ");
        // calling the PrintIndexAndValues()
        // method to print
        PrintIndexAndValues(bytes);
        // print char value
        Console.WriteLine("index     byte Array           int value");
        for (int index = 0; index < bytes.Length - 3;
                              index = index + 4) {
            int values = BitConverter.ToInt16(bytes, index);
            Console.WriteLine(" {0}     {1}         {2}",
             index, BitConverter.ToString(bytes, index,
                                           4), values);
        }
    }
    catch (ArgumentNullException e) {
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }
    catch (ArgumentOutOfRangeException e) {
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }
    catch (ArgumentException e) {
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }
}
// Defining the method
// PrintIndexAndValues
public static void PrintIndexAndValues(byte[] myArr)
{
    for (int i = 0; i < myArr.Length; i++) {
        Console.Write("{0} ", myArr[i]);
    }
    Console.WriteLine();
    Console.WriteLine();
}
}

输出:

Initial Array: 32 0 0 42 0 65 0 125 0 197 0 168 3 41 4 125 32
index byte Array int value
0 20-00-00-2A 32
4 00-41-00-7D 16640
8 00-C5-00-A8 -15104
12 03-29-04-7D 10499

例 2:议论文异常

c sharp . c sharp . c sharp . c sharp


// C# program to demonstrate
// BitConverter.ToInt32(Byte[], Int32);
// Method
using System;
class GFG {
// Main Method
public static void Main()
{
    try {
        // Define an array
        // of byte values.
        byte[] bytes = {32, 0, 0, 42, 0,
                        65, 0, 125, 0, 197,
                        0, 168, 3, 41, 4, 125};
        // Display the values of the myArr.
        Console.Write("Initial Array: ");
        // calling the PrintIndexAndValues()
        // method to print
        PrintIndexAndValues(bytes);
        // print char value
        Console.WriteLine("index      element          int value");
        for (int index = 1; index < bytes.Length - 2;
                                 index = index + 4) {
            if (index == bytes.Length - 3) {
                Console.WriteLine();
                Console.WriteLine("startIndex equals the "+
                               "length of value minus 3.");
                int values = BitConverter.ToInt32(bytes, index);
                Console.WriteLine("  {0}      {1}         {2}",
                 index, BitConverter.ToString(bytes, index, 4), values);
            }
            else {
                int values = BitConverter.ToInt32(bytes, index);
                Console.WriteLine("  {0}      {1}         {2}",
                 index, BitConverter.ToString(bytes, index, 4), values);
            }
        }
    }
    catch (ArgumentNullException e) {
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }
    catch (ArgumentOutOfRangeException e) {
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }
    catch (ArgumentException e) {
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }
}
// Defining the method
// PrintIndexAndValues
public static void PrintIndexAndValues(byte[] myArr)
{
    for (int i = 0; i < myArr.Length; i++) {
        Console.Write("{0} ", myArr[i]);
    }
    Console.WriteLine();
    Console.WriteLine();
    Console.WriteLine("initial Array in string: {0} ",
                       BitConverter.ToString(myArr));
    Console.WriteLine();
}
}

输出:

Initial Array: 32 0 0 42 0 65 0 125 0 197 0 168 3 41 4 125
initial Array in string: 20-00-00-2A-00-41-00-7D-00-C5-00-A8-03-29-04-7D
index element int value
1 00-00-2A-00 2752512
5 41-00-7D-00 8192065
9 C5-00-A8-03 61341893
startIndex equals the length of value minus 3.
Exception Thrown: System.ArgumentException

例 3:argumentout of rangeexception

c sharp . c sharp . c sharp . c sharp


// C# program to demonstrate
// BitConverter.ToInt32(Byte[], Int32);
// Method
using System;
class GFG {
// Main Method
public static void Main()
{
    try {
        // Define an array
        // of byte values.
        byte[] bytes = {32, 0, 0, 42, 0, 65,
                        0, 125, 0, 197, 0,
                        168, 3, 41, 4, 125};
        // Display the values of the myArr.
        Console.Write("Initial Array: ");
        // calling the PrintIndexAndValues()
        // method to print
        PrintIndexAndValues(bytes);
        // print char value
        Console.WriteLine("index      element          int value");
        for (int index = 0; index < bytes.Length + 1;
                                index = index + 4) {
            if (index == bytes.Length) {
                Console.WriteLine();
                Console.WriteLine("startIndex is greater than "+
                                 "the length of value minus 1");
                int values = BitConverter.ToInt32(bytes, index);
                Console.WriteLine("  {0}      {1}         {2}",
                 index, BitConverter.ToString(bytes, index, 4), values);
            }
            else {
                int values = BitConverter.ToInt32(bytes, index);
                Console.WriteLine("  {0}      {1}         {2}",
                 index, BitConverter.ToString(bytes, index, 4), values);
            }
        }
    }
    catch (ArgumentNullException e) {
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }
    catch (ArgumentOutOfRangeException e) {
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }
    catch (ArgumentException e) {
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }
}
// Defining the method
// PrintIndexAndValues
public static void PrintIndexAndValues(byte[] myArr)
{
    for (int i = 0; i < myArr.Length; i++) {
        Console.Write("{0} ", myArr[i]);
    }
    Console.WriteLine();
    Console.WriteLine();
    Console.WriteLine("initial Array in string: {0} ",
                       BitConverter.ToString(myArr));
    Console.WriteLine();
}
}

输出:

Initial Array: 32 0 0 42 0 65 0 125 0 197 0 168 3 41 4 125
initial Array in string: 20-00-00-2A-00-41-00-7D-00-C5-00-A8-03-29-04-7D
index element int value
0 20-00-00-2A 704643104
4 00-41-00-7D 2097168640
8 00-C5-00-A8 -1476344576
12 03-29-04-7D 2097424643
startIndex is greater than the length of value minus 1
Exception Thrown: System.ArgumentOutOfRangeException

例 4:ArgumentNullException

c sharp . c sharp . c sharp . c sharp


// C# program to demonstrate
// BitConverter.ToInt32(Byte[], Int32);
// Method
using System;
class GFG {
// Main Method
public static void Main()
{
    try {
        // Define an array of byte values.
        byte[] bytes = null;
        // get the int value
        int values = BitConverter.ToInt32(bytes, 0);
        Console.Write("{0}", values);
    }
    catch (ArgumentNullException e) {
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }
    catch (ArgumentOutOfRangeException e) {
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }
    catch (ArgumentException e) {
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }
}
}

输出:

Exception Thrown: System.ArgumentNullException

参考:


  • https://docs . Microsoft . com/en-us/dotnet/API/system . bit converter . toint 32?视图=netframework-4.7.2


推荐阅读
  • 本文介绍了闭包的定义和运转机制,重点解释了闭包如何能够接触外部函数的作用域中的变量。通过词法作用域的查找规则,闭包可以访问外部函数的作用域。同时还提到了闭包的作用和影响。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 计算机存储系统的层次结构及其优势
    本文介绍了计算机存储系统的层次结构,包括高速缓存、主存储器和辅助存储器三个层次。通过分层存储数据可以提高程序的执行效率。计算机存储系统的层次结构将各种不同存储容量、存取速度和价格的存储器有机组合成整体,形成可寻址存储空间比主存储器空间大得多的存储整体。由于辅助存储器容量大、价格低,使得整体存储系统的平均价格降低。同时,高速缓存的存取速度可以和CPU的工作速度相匹配,进一步提高程序执行效率。 ... [详细]
  • 本文介绍了Python爬虫技术基础篇面向对象高级编程(中)中的多重继承概念。通过继承,子类可以扩展父类的功能。文章以动物类层次的设计为例,讨论了按照不同分类方式设计类层次的复杂性和多重继承的优势。最后给出了哺乳动物和鸟类的设计示例,以及能跑、能飞、宠物类和非宠物类的增加对类数量的影响。 ... [详细]
  • 使用nodejs爬取b站番剧数据,计算最佳追番推荐
    本文介绍了如何使用nodejs爬取b站番剧数据,并通过计算得出最佳追番推荐。通过调用相关接口获取番剧数据和评分数据,以及使用相应的算法进行计算。该方法可以帮助用户找到适合自己的番剧进行观看。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • Commit1ced2a7433ea8937a1b260ea65d708f32ca7c95eintroduceda+Clonetraitboundtom ... [详细]
  • Linux环境变量函数getenv、putenv、setenv和unsetenv详解
    本文详细解释了Linux中的环境变量函数getenv、putenv、setenv和unsetenv的用法和功能。通过使用这些函数,可以获取、设置和删除环境变量的值。同时给出了相应的函数原型、参数说明和返回值。通过示例代码演示了如何使用getenv函数获取环境变量的值,并打印出来。 ... [详细]
  • 本文介绍了PE文件结构中的导出表的解析方法,包括获取区段头表、遍历查找所在的区段等步骤。通过该方法可以准确地解析PE文件中的导出表信息。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • 本文讨论了如何在dotnet桌面(Windows)应用程序中添加图标。作者提到可以使用dotnet命令行工具与resource.rc文件一起使用来为标准.NET核心应用程序添加图标。作者还介绍了在创建控制台应用程序时如何编辑projeto1.csproj文件来添加图标。 ... [详细]
  • 本文介绍了深入浅出Linux设备驱动编程的重要性,以及两种加载和删除Linux内核模块的方法。通过一个内核模块的例子,展示了模块的编译和加载过程,并讨论了模块对内核大小的控制。深入理解Linux设备驱动编程对于开发者来说非常重要。 ... [详细]
  • 本文介绍了在iOS开发中使用UITextField实现字符限制的方法,包括利用代理方法和使用BNTextField-Limit库的实现策略。通过这些方法,开发者可以方便地限制UITextField的字符个数和输入规则。 ... [详细]
author-avatar
910621rh_270
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有